Add zero-copy unpack_type overload for std::span<B const>#7
Merged
rwindegger merged 1 commit intorwindegger:mainfrom Mar 4, 2026
Merged
Conversation
## Problem
The existing binary unpack path copies data into std::vector<B>:
value.assign(src, src + bin_size); // always allocates + copies
For callers that only need to read the binary payload (e.g. pass it to a
parser like GEOS WKBReader), this allocation is pure waste — the data is
already contiguous in the Unpacker's internal buffer.
## Solution
Add a second unpack_type overload that resolves when the caller declares
their argument as std::span<B const>:
template<std::size_t E>
void unpack_type(std::span<B const, E> &value) {
// ... read header to get bin_size ...
value = data_.subspan(position_, bin_size); // zero-copy view
increment(bin_size);
}
data_.subspan() returns a non-owning view into the Unpacker's own buffer,
so no allocation or memcpy occurs. The span remains valid for the lifetime
of the Unpacker (i.e. for the duration of the unpack call chain), which
is sufficient for immediate-use patterns like deserialise → parse → discard.
## Safety
- Bounds are checked identically to the vector overload (position_ + bin_size
> data_.size() throws std::out_of_range).
- The returned span is const-qualified (B const), preventing accidental writes
back into the source buffer.
- Callers that need to own the data should continue using std::vector<B>.
## Backwards compatibility
Fully additive — existing code using std::vector<B> is unaffected. The new
overload is only selected when the target variable is declared as std::span.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #7 +/- ##
==========================================
+ Coverage 93.16% 93.90% +0.74%
==========================================
Files 1 1
Lines 468 476 +8
==========================================
+ Hits 436 447 +11
+ Misses 32 29 -3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The existing binary unpack path copies data into std::vector:
For callers that only need to read the binary payload (e.g. pass it to a parser like GEOS WKBReader), this allocation is pure waste — the data is already contiguous in the Unpacker's internal buffer.
Solution
Add a second unpack_type overload that resolves when the caller declares their argument as std::span:
data_.subspan() returns a non-owning view into the Unpacker's own buffer, so no allocation or memcpy occurs. The span remains valid for the lifetime of the Unpacker (i.e. for the duration of the unpack call chain), which is sufficient for immediate-use patterns like deserialise → parse → discard.
Safety
Backwards compatibility
Fully additive — existing code using std::vector is unaffected. The new overload is only selected when the target variable is declared as std::span.